package com.example.administrator.kotlin.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.adapter.NewsAdapter1
import com.example.administrator.kotlin.bean.NewsBean
import com.example.administrator.kotlin.bean.Newslist
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.orhanobut.logger.Logger

class BrvahActivity : AppCompatActivity() {
    private var TAG: String = BrvahActivity::class.java.simpleName
    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: NewsAdapter1
    private var datas: List<Newslist>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_brvah)
        recyclerView = findViewById<RecyclerView>(R.id.recyclerview_news)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = NewsAdapter1(R.layout.item_news, datas)
        recyclerView.adapter = adapter

        //获取数据
        initData()
        initClickListener()
    }

    private fun initClickListener() {

    }

    private fun initData() {
        var url = "http://api.tianapi.com/social/?key=861a794733dd2b057e415d822bef9586&num=10"
        OkGo.get<String>(url)
                .tag("aha")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        Logger.i(TAG, response?.body().toString())
                        var gson: Gson = Gson()
                        val newsBean = gson.fromJson<NewsBean>(response?.body(), NewsBean::class.java)
                        adapter.setNewData(newsBean.newslist)
                    }

                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                    }

                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                    }

                    override fun onFinish() {
                        super.onFinish()
                    }
                })
    }

    override fun onDestroy() {
        super.onDestroy()
//        OkGo.cancelTag(OkHttpClient(),"tag")
    }
}
